$(function () {

    // 查询分布数据
    questionGetPage();
    


})

function questionGetPage() {

    const keyWord = document.getElementById('keyWord');
    var token = localStorage.getItem("token");  

    const pageSize = 10;
    const pageNo = 1;

    fetch('http://localhost:8080/questionGetPage', {
        method: 'GET',
        headers: {  
            'Authorization': token  // 如果需要 Bearer 前缀  
        },
        data: JSON.stringify({
            keyWord: keyWord,
            // 一页有好多条
            pageSize:pageSize,
            // 当前第几页
            pageNo:pageNo
        }),
    })
    .then(response => {
        if(response.ok) {
            return response.json(); // 将响应转换为 JSON 格式
        } else {
            throw new Error('文件上传失败');
        }
    })
    .then(response => {

        var objects = response.data.records;


        var resultContent = '';

        for (const key in objects) {

            const obj = objects[key];
            
            resultContent += `
            <div class="content">
                <img src="http://localhost:8080/download/ `+obj.avatar+` height="70" width="70" style="position:relative;left: 5px;top: 2px;">
                <a href="" class="one">`+obj.title+`</a>
                <a style="font-size: 10px;position:relative;top: -10px;right: 10px;">
                    `+obj.username+`
                </a>
                <a style="position:relative;top: -10px;">
                    <img src="imang/点.jpg" height="6" width="6">
                </a>
                <a style="font-size: 10px;position:relative;top: -10px;left: 5px;">
                    `+obj.creatTime+`
                </a>
            </div>
            `;
        }

        const publishPageData = document.getElementById('questions_and_answers_id');

        publishPageData.innerHTML = resultContent;


        const currentPage = document.getElementById('currentPage');
     
        const total = document.getElementById('total');

        currentPage.innerText  = response.data.current;

        total.innerText   = '共'+response.data.total+'条';
    })
    .catch(error => {
        console.error('查询异常:', error);
    });
}